package com.hula.onion.book.server.common;

import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.client.BasicCookieStore;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Description:
 * Create Date:2018/1/17 19:30
 *
 * @author liangbing
 * @version 1.0
 */
public abstract class SpiderCommon {

    protected Logger logger = null;

    private static final ThreadLocal<HttpClientContext> threadLocal =
            new ThreadLocal() {
                @Override
                protected Object initialValue() {
                    BasicCookieStore cookieStore = new BasicCookieStore();
                    HttpClientContext httpClientContext = new HttpClientContext();
                    httpClientContext.setCookieStore(cookieStore);
                    return httpClientContext;
                }
            };

    public SpiderCommon() {
        logger = LoggerFactory.getLogger(getClass().getName());
    }

    /**
     * 登陆表单
     */
    protected LoginForm loginForm;


    public void setLoginForm(LoginForm loginForm) {
        this.loginForm = loginForm;
    }


    /**
     * 获取线程私有的http上线文 并设置CookieStore
     *
     * @return
     */
    public HttpClientContext getHttpContext() {
        return threadLocal.get();
    }

    /**
     * 执行爬取方法
     *
     * @return
     */
    public abstract CommonResponse execute();

    /**
     * 获取验证码URL，由子类覆盖
     *
     * @return
     */
    public String getCaptchaURL() {
        return null;
    }

    /**
     * 获取登陆页地址
     *
     * @return
     */
    public String getLoginPageURL() {
        return null;
    }

    /**
     * 获取首页信息
     *
     * @param html 首页html
     */
    public void getIndexPageInfo(String html, int platformId, String userFlag) {

    }
}
